<?php

class Mimage extends TST_Model {

    protected $table_products = "products";
    protected $table_ = "category";
    protected $table_images = "images";
    protected $selected_columns = "*";

    function Mimage() {
        parent::TST_Model();
    }

    public function getAllImagesByProductUrl($product_url = null, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT pi.id, pi.name                                   
                    FROM {$this->table_images} pi INNER JOIN {$this->table_products} p ON pi.product_id = p.id
                    WHERE pi.status = ?
                    AND   p.friendly_url = ?
                    AND  pi.image_type = 'product'
                    ORDER BY pi.in_order, pi.name ";
        return $this->query_array($sql, array($status, $product_url));
    }

    public function getAllImageBySlide($language, $type, $status) {
        $sql = "SELECT name, description, product_id                                    
                    FROM {$this->table_image}
                    WHERE language = ? 
                    AND   status = ?                    
                    AND   image_type = ?
                    ORDER BY in_order, name ";
        return $this->query_array($sql, array($language, $status, $type));
    }

//for Admin----------------------------------------------------------------------

    public function getImageSlideById($language, $id, $type) {
        $sql = "SELECT i.*, p.name AS product_name                                  
                    FROM {$this->table_image} as i LEFT JOIN {$this->table_product} as p ON (i.product_id = p.id)
                    WHERE i.language = ? 
                    AND   i.id = ?
                    AND   i.status NOT LIKE 'delete'
                    AND   i.image_type = ?
                    ORDER BY i.in_order, i.name ";
        return $this->query_row($sql, array($language, $id, $type));
    }

    public function getAllImageSlide($language, $type) {
        $sql = "SELECT i.*, p.name AS product_name                                  
                    FROM {$this->table_image} as i LEFT JOIN {$this->table_product} as p ON (i.product_id = p.id)
                    WHERE i.language = ? 
                    AND   i.status NOT LIKE 'delete'
                    AND   i.image_type = ?
                    ORDER BY i.in_order, i.name ";
        return $this->query_array($sql, array($language, $type));
    }

    public function getAllImageByProductIDAd($language, $id, $type) {
        $sql = "SELECT *                                   
                    FROM {$this->table_image}
                    WHERE language = ? 
                    AND   status NOT LIKE 'delete'
                    AND   product_id = ?
                    AND   image_type = ?
                    ORDER BY in_order, name ";
        return $this->query_array($sql, array($language, $id, $type));
    }

    public function update($id, $data) {
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_image, $data, $where);
        return $this->db->query($sql);
    }

    public function addNew($data) {
        $this->db->insert($this->table_image, $data);
        return $this->db->insert_id();
    }

    public function delete($id) {
        $data = array(
            'status' => 'delete'
        );
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_image, $data, $where);
        return $this->db->query($sql);
    }

}
